package util.general;

import java.io.FileInputStream; 
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties; 
import util.i3enum.*;
 
 
/**
 * 
 * @author ThaiBaoDoan
 *
 */
public class  config_general {
	  
	public static config_path Read_CurrentDir() 
	{
		Properties applicationProps = new Properties();  
		config_path result = new config_path();
		try{
			FileInputStream in = new FileInputStream(config_path.CONFIG_DIRECTORY_PATH);
			applicationProps.load(in);
			result.sLeftPath       = applicationProps.getProperty(config_path.CONFIG_KEY_PLEFT);
			result.sRightPath      = applicationProps.getProperty(config_path.CONFIG_KEY_PRIGHT);
			result.sLeftDirectory  = applicationProps.getProperty(config_path.CONFIG_KEY_DLEFT);
			result.sRightDirectory = applicationProps.getProperty(config_path.CONFIG_KEY_DRIGHT);
			in.close();
			
		}
		catch(Exception e)
		{
			//If property isn't exist , create new property
			try{
			    config_path newconfig = new config_path();
				FileOutputStream out = new FileOutputStream(config_path.CONFIG_DIRECTORY_PATH);
				Properties applicationPropsOut = new Properties();
				applicationPropsOut.setProperty(config_path.CONFIG_KEY_PLEFT,newconfig.sLeftPath);
				applicationPropsOut.setProperty(config_path.CONFIG_KEY_PRIGHT,newconfig.sRightPath);
				applicationPropsOut.setProperty(config_path.CONFIG_KEY_DLEFT,newconfig.sLeftDirectory);
				applicationPropsOut.setProperty(config_path.CONFIG_KEY_DRIGHT,newconfig.sRightDirectory);
				applicationPropsOut.store(out, config_path.CONFIG_NAME);
				out.close();
			}
			catch (IOException ioex)
			{
				System.err.println(ioex);
			}
		}
		
		return result;
		
	}
	/**
	 *  Write current directory in left & right main pannel
	 * @param config_path
	 */
	
	public static void Write_CurrentDir(config_path config_path) 
	{
		try{ 
				FileOutputStream out = new FileOutputStream(util.i3enum.config_path.CONFIG_DIRECTORY_PATH);
				Properties applicationPropsOut = new Properties();
				applicationPropsOut.setProperty(util.i3enum.config_path.CONFIG_KEY_PLEFT,config_path.sLeftPath);
				applicationPropsOut.setProperty(util.i3enum.config_path.CONFIG_KEY_PRIGHT,config_path.sRightPath);
				applicationPropsOut.setProperty(util.i3enum.config_path.CONFIG_KEY_DLEFT,config_path.sLeftDirectory);
				applicationPropsOut.setProperty(util.i3enum.config_path.CONFIG_KEY_DRIGHT,config_path.sRightDirectory);
				applicationPropsOut.store(out, util.i3enum.config_path.CONFIG_NAME);
				out.close();
		}
		catch (IOException ioex)
		{
			System.err.println(ioex);
		}
	}
	public static FileType GetPartitionType(String _path)
	{
		int _nFileType    =   Kernel32.INSTANCE.GetDriveType(_path);
		FileType filetype;
		switch(_nFileType)
		{
		 case 0:
		 case 1:
			 filetype = FileType.PARTITION_UNKNOWN;
			 break;
		 case 2:
			 filetype = FileType.PARTITION_REMOVEDISK;
			 break;
		 case 3:
			 filetype = FileType.PARTITION_HARDDISK;
			 if((Kernel32.INSTANCE.GetFileAttributes(_path) & Kernel32.FILE_ATTRIBUTE_ENCRYPTED )!=0)
			 {
				 filetype = FileType.PARTITION_HARDDISK_ENCRYPTION;
			 }
			 break;
		 case 4:
			 filetype = FileType.PARTITION_NETWORK;
			 break;
		 case 5:
			 filetype = FileType.PARTITION_CD_DVD;
			 break;
		default:
			filetype  = FileType.FILE_NOMAL;
			break;
		}
		return filetype;
	}
	public static FileType GetFileAttibutes(String _path)
	{
		FileType filetype = FileType.FILE_NOMAL;
		
		return filetype;
	}
	public static boolean IsSystemFile(String _path)
	{
		int _nFileType    =   Kernel32.INSTANCE.GetFileAttributes(_path);
		if( (_nFileType & Kernel32.FILE_ATTRIBUTE_OS) !=0 )
				 
			return true; 
		return false;
	}
}
